<?php
namespace Home\Controller;

class MemberController extends CommonController
{

	/**
	 * [index 展现用户个人中心的首页]
	 * @return [type] [description]
	 */
	public function index()
	{
		$user_info = M('user_info');
		// $username = 
		$uid['uid'] = ['eq',session('user')['id']];
		$list['user_info'] = $user_info->where($uid)->find();
		$this->assign('user',$list['user_info']);

		$arr = [1=>"男",2=>"女"];
		$user = M('user');
		$map['id'] = ['eq',session('user')['id']];


		$list['user'] = $user->where($map)->find();
		$this->assign('arr',$arr);
		$this->assign('list',$list);
		
		$this->display("Member_User");
	}

	/**
	 * [Member_Address 查看所有收货地址的信息]
	 */
	public function Member_Address()
	{
		$address = D('address');
		$res = $address->address_sel();
		//分配搜索出的数据库的所有数据
		$this->assign($res);

		$this->display();
	}

	/**
	 * [member_eidt 调用增加或修改地址的方法]
	 * @return [type] [正确与否输出]
	 */
	public function member_eidt()
	{
		$address = D('address');

		$res = $address->address_eidt();
		if($res!='success'){
			return $this->error($res,'',2);
		}

		$this->redirect("Member_Address");
	}

	/**
	 * [member_status 默认状态的修改]
	 * @return [type] [description]
	 */
	public function member_status()
	{
		$status = D('address');
		$res = $status->address_status();
	}
	/**
	 * [member_del 实现删除地址的功能]
	 * @return [type] [description]
	 */
	public function member_del()
	{
		$id = I('get.id');
		$user = M('address');

		$res = $user->delete($id);
	}

	/**
	 * [safe_index 点击左侧栏跳转到账号安全页面]
	 * @return [type] [description]
	 */
	public function safe_index()
	{
		$this->display("Member_safe");
	}

	/**
	 * [safe_phone 账户安全栏的修改手机号码]
	 * @return [type] [description]
	 */
	public function safe_phone()
	{
		$user = D('address');
		$res = $user->address_Safephone();
		/*dump($res);
		exit;*/
		if($res=="none"){
			echo "<script>alert('手机号码与原手机号码不符!')</script>";
		}

		if($res=="no"){
			echo "<script>alert('请输入正确的电话号码!')</script>";
		}

		if($res=="success"){
			echo "<script>alert('修改成功!')</script>";
		}
		$this->display("Member_safe");
	}

	/**
	 * [safe_pass 账户安全栏修改密码]
	 * @return [type] [description]
	 */
	public function safe_pass()
	{
		$user = D('address');
		$res = $user->address_pass();
		dump($res);
		if($res=="none"){
			echo "<script>alert('登录密码不正确!')</script>";
		}

		if($res=='no'){
			echo "<script>alert('密码不合规!')</script>";
		}

		if($res=="ov"){
			echo "<script>alert('两次密码输入不一致!')</script>";
		}

		if($res=="over"){
			echo "<script>alert('修改失败!')</script>";
		}

		if($res=="success"){
			echo "<script>alert('修改成功!')</script>";
		}

		$this->display("Member_safe");
	}

	/**
	 * [safe_email 账号安全栏修改邮箱]
	 * @return [type] [description]
	 */
	public function safe_email()
	{
		$user = D('address');
		$res = $user->address_email();
		/*dump($res);
		exit;*/
		if($res=="none"){
			echo "<script>alert('与原邮箱不符')</script>";
		}

		if($res=="no"){
			echo "<script>alert('新邮箱输入不正确')</script>"; 
		}

		if($res=="n"){
			echo "<script>alert('该邮箱已被其他账号所注册')</script>"; 
		}

		if($res=="error"){
			echo "<script>alert('由于系统原因，修改失败')</script>";
		}

		if($res=="success"){
			echo "<script>alert('修改成功')</script>";
		}

		$this->display("Member_safe");
	}

	/**
	 * [add 添加用户信息]
	 */
	public function add()
	{

		$user = D('address');
		$name = I('post.name');
		//调用上传类上传用户图片
		$res = self::uploads();
		if ($res=="miss") {
			echo "<script>alert('图片上传失败！')</script>";
		}
		//发送上传后的图片名到model写入数据库
		$res = $user->member_add($res['pic']['savename']);
		//防止昵称为空
		if($res=="kong"){
			echo "<script>alert('请输入昵称')</script>";
		}
		//更新资料成功提示
		if($res=="success"){
			echo "<script>alert('更新成功')</script>";
		}
		//更新失败成功提示
		if($res=="over"){
			echo "<script>alert('更新失败')</script>";
		}
		//把用户输入的资料写入到数据库成功提示
		if($res=="su"){
			echo "<script>alert('资料添加成功')</script>";
		}

		if($res=="ov"){
			echo "<script>alert('资料添加失败')</script>";
		}
		//调用本类的index方法返回页面
		$this->index();
	}

	/**
	 * [uploads 上传图片类]
	 * @return [type] [description]
	 */
	public function uploads()
	{
		if($_FILES['pic']['error']==4){
			return;
		}else{
			$upload = new \Think\Upload();
			$upload->maxSize = 3145728;
			$upload->exts=array('jpg', 'gif', 'png', 'jpeg');
			$upload->savePath= 'user/'; 
			$upload->saveName = time().'_'.mt_rand();
			$upload->autoSub  = false;
			$info = $upload->upload();
			
			if(!$info){
				return "miss";
			}
			return $info;
		}
	}

	/**
	 * [Member_Collect 展示收藏列表]
	 */
	public function Member_Collect()
	{
		$collect = D('address');
		$res = $collect->Collect_show();
		
		$this->assign($res);
		$this->display('member_collect');
	}
	/**
	 * [do_keep 取消收藏功能]
	 * @return [type] [接收收藏表和商品表的信息]
	 */
	public function do_keep(){
		$id = I('get.gid');
		$user = M('keep');
		$map['gid'] = ['eq',$id];
		$map['uid'] = ['eq',session('user')['id']];
		$res = $user->where($map)->delete();
		$this->Member_Collect();
	}

	//====对应我的评论栏=========================================
	public function Member_comment()
	{
		$user = D('address');
		$res = $user->commemt_show();
		$this->assign("list",$res);
		$this->display('Member_comment');
	}
	/**
	 * [do_comment 删除我的评论功能]
	 * @return [type] [description]
	 */
	public function do_comment()
	{
		$id = I('get.id');
		$map['id'] = ['eq',$id];
		$user = M('comment');
		$map['uid'] = ['eq',session('user')['id']];
		$res = $user->where($map)->delete();
		$this->Member_comment();
	}


	//====对应我的订单栏================================
	/**
	 * [order_show 查看订单]
	 * @return [type] [description]
	 */
	public function member_order()
	{
		$user = D('address');
		$res = $user->order_show();
		$this->assign('order',$res);
		$this->display('member_order');
		
	}

	public function order_del()
	{
		$id = I('get.id');
		$user = M('orders');
		$map['id'] = ['eq',$id];
		$list = $user->where($map)->find();

		switch ($list['status']) {
			case '1':
				$data['status']=3;
				$res = $user->where($map)->save($data);
				if($res){
					echo "1";
				}
				break;
			case '2':
				echo "2";
				break;
			case '3':
				echo "3";
				break;
			case '4':
				echo "4";
				break;
		}
	}
	/**
	 * [order_success 修改订单状态]
	 * @return [type] [description]
	 */
	public function order_success()
	{
		$id = I('get.id');
		$user = M('orders');
		$map['id'] = ['eq',$id];
		$list = $user->where($map)->find();
		
		if($list['status'] == 2){
			$data['status']=4;
			// $user = M('orders');
			$res = $user->where($map)->save($data);
			echo "success";
		}
		
	}

	 //=============2017_1_23  个人中心订单详情=====================
	public function Member_orderdetail()
	{
		//获取订单表id
		$orderid = I('get.id');
		// dump($orderid);
		//查询该订单详情
			//--设置查询条件--以订单id作为查询条件
		$map['orderid'] = ['eq',$orderid];
		//执行并接收返回值
			//实例化order_info 表
		$orderinfo = M('order_info');
		$list = $orderinfo->where($map)->select();
		// dump($list_goods);	
		//取得详情表里面的goodsid
		$num = count($list);
		//实例化goods表
		$goods = M('goods');
		for ($i=0; $i < $num; $i++) { 
			$goodsid = $list[$i]['goodsid'];
			$map = [];
			$map['id'] = ['eq',$goodsid]; 
			$glist[] = $goods->where($map)->find();
			$list[$i]['goodsname'] = $glist[$i]['goods'];
			$list[$i]['picname'] = $glist[$i]['pic'];
		}
		// dump($list); 	
		$this->assign('list',$list);
		$this->display();
	}
}